#ifndef __PLANE_H__
#define __PLANE_H__

#include "common.h"

/*
  x-y plane
  center (0.0.1)
  edge as 2 unit
*/
class Plane
{
public:
  static Plane&  Create(GLfloat xPos, GLfloat yPos, GLfloat zPos, GLfloat width, GLfloat height, GLuint wUnits, GLuint hUnits)
  {
    Plane* ptr = new Plane;
    return *ptr;
  }

  static void Destroy(Plane* objPtr)
  {
    delete objPtr;
  }

  void Apply()
  {
    // apply vertex data
    glEnableClientState( GL_VERTEX_ARRAY );
    glBindBuffer( GL_ARRAY_BUFFER, _vtx );
    glVertexPointer( 3, GL_FLOAT, 0, bufObjPtr( 0 ) );
    // apply normal data
    glEnableClientState( GL_NORMAL_ARRAY );
    glBindBuffer( GL_ARRAY_BUFFER, _nor );
    glNormalPointer(GL_FLOAT, 0, bufObjPtr(0) );
    // apply index data
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _idx);
  }

  GLuint GetVertexCount() const { return _vtxCnt; }
  GLuint GetIndexCount() const { return _idxCnt; }
protected:
  Plane()
  {
    _Init();
  }
  ~Plane()
  {
    _Term();
  }
private:
  void _Init()
  {
    GLfloat vertex[]=
    {
    -1.0f, -1.0f, 1.0f,
    1.0f, -1.0f, 1.0f,
    1.0f,  1.0f, 1.0f,
    -1.0f,  1.0f, 1.0f
    };

    GLfloat normal[]=
    {
      0,0,1.f // face normal
    };

    GLushort index[]=
    {
      0,1,2,3
    };

    _vtxCnt = 4;
    _idxCnt = 4;

    // vertex data to VRAM
    glGenBuffers(1, &_vtx);
    glBindBuffer(GL_ARRAY_BUFFER, _vtx);
    glBufferData(GL_ARRAY_BUFFER, _vtxCnt*3*sizeof(GLfloat), (void*)vertex, GL_STATIC_DRAW);

    // normal data to VRAM
    glGenBuffers(1, &_nor);
    glBindBuffer( GL_ARRAY_BUFFER, _nor);
    glBufferData(GL_ARRAY_BUFFER, 1*3*sizeof(GLfloat), (void*)normal, GL_STATIC_DRAW);

    // index data to VRAM
    glGenBuffers(1, &_idx);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _idx);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, _idxCnt*sizeof(GLushort), (void*)index, GL_STATIC_DRAW);
  }

  void _Term()
  {
    glDeleteBuffers(1, &_vtx);
    glDeleteBuffers(1, &_nor);
    glDeleteBuffers(1, &_idx);
  }

  GLuint _vtx;
  GLuint _nor;
  GLuint _idx;
  //GLuint _texCoord;
  //GLuint _tex;

  GLuint _vtxCnt;
  GLuint _idxCnt;
};

#endif //__PLANE_H__